package com.yilin.medical.utils;

import java.io.IOException;
import java.lang.reflect.Field;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Rect;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

/**
 * Created by gmx on 2014/7/22.
 */
public class JxSystemUtil {

	public static final int NETWORK_NOT = 0;
	public static final int NETWORK_WIFI = 1;
	public static final int NETWORK_2G = 2;
	public static final int NETWORK_3G = 3;

	/**
	 * 判断当前是否有可用的网络以及网络类型 0：无网络 1：WIFI 2：2G 3：3G
	 * 
	 * @param context
	 * @return
	 */
	public static int isNetworkAvailable(Context context) {
		if (context == null) {
			return NETWORK_NOT;
		}
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity == null) {
			return NETWORK_NOT;
		} else {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						NetworkInfo netWorkInfo = info[i];
						if (netWorkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
							return NETWORK_WIFI;
						} else if (netWorkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
							// String extraInfo = netWorkInfo.getExtraInfo();
							// if ("cmwap".equalsIgnoreCase(extraInfo)
							// || "cmwap:gsm".equalsIgnoreCase(extraInfo)) {
							// return 2;
							// }
							if (netWorkInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_GPRS
									|| netWorkInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_CDMA
									|| netWorkInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_EDGE) {
								return NETWORK_2G;
							}
							return NETWORK_3G;
						}
					}
				}
			}
		}
		return NETWORK_NOT;
	}

	/**
	 * 获取版本名称
	 * 
	 * @author gmx1234@gmail.com
	 * @return
	 * @return String
	 */
	public static String getAppVersionNumber(Context context) {
		return getAppVersionNumber(context, null);
	}

	/**
	 * 获取版本名称
	 * 
	 * @author gmx1234@gmail.com
	 * @param packageName
	 * @return
	 * @return String
	 */
	public static String getAppVersionNumber(Context context, String packageName) {
		String versionName;

		if (packageName == null) {
			packageName = context.getPackageName();
		}

		try {
			PackageManager packageManager = context.getPackageManager();
			PackageInfo packageInfo = packageManager.getPackageInfo(
					packageName, 0);
			versionName = packageInfo.versionName;
		} catch (Exception e) {
			versionName = "";
		}

		return versionName;
	}

	/**
	 * 获取应用程序的版本号
	 * 
	 * @author gmx1234@gmail.com
	 * @return
	 * @return String
	 */
	public static String getAppVersionCode(Context context) {
		return getAppVersionCode(context, null);
	}

	/**
	 * 获取指定应用程序的版本号
	 * 
	 * @author gmx1234@gmail.com
	 * @param packageName
	 * @return
	 * @return String
	 */
	public static String getAppVersionCode(Context context, String packageName) {
		String versionCode;

		if (packageName == null) {
			packageName = context.getPackageName();
		}
		try {
			PackageManager packageManager = context.getPackageManager();
			PackageInfo packageInfo = packageManager.getPackageInfo(
					packageName, 0);
			versionCode = Integer.toString(packageInfo.versionCode);
		} catch (Exception e) {
			versionCode = "";
		}

		return versionCode;
	}

	private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
	private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
	private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";

	/**
	 * 是否是小米系统
	 * 
	 * @return
	 */
	public static boolean isMIUI() {
		try {
			final BuildProperties prop = BuildProperties.newInstance();
			return prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null
					|| prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null
					|| prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null;
		} catch (final IOException e) {
			return false;
		}
	}

	/**
	 * 获取状态栏高度
	 */
	public static int getStatusBarHeight(Activity activity) {
		int statusBarHeight = 0;
		try {
			Class<?> c = Class.forName("com.android.internal.R$dimen");
			Object o = c.newInstance();
			Field field = c.getField("status_bar_height");
			int x = (Integer) field.get(o);
			statusBarHeight = activity.getResources().getDimensionPixelSize(x);
		} catch (Exception e) {
			e.printStackTrace();
			Rect frame = new Rect();
			activity.getWindow().getDecorView()
					.getWindowVisibleDisplayFrame(frame);
			statusBarHeight = frame.top;
		}
		return statusBarHeight;
	}

	/**
	 * 手写判断是否是Flyme OsS 4.x.x
	 */
	public static boolean isFlymeOs4x() {
		String sysVersion = android.os.Build.VERSION.RELEASE;
		if ("4.4.4".equals(sysVersion)) {
			String sysIncrement = android.os.Build.VERSION.INCREMENTAL;
			String displayId = android.os.Build.DISPLAY;
			if (!TextUtils.isEmpty(sysIncrement)) {
				return sysIncrement.contains("Flyme_OS_4");
			} else {
				return displayId.contains("Flyme OS 4");
			}
		}
		return false;
	}

}
